// src/stores/user.js
import { defineStore } from 'pinia'
import { ref, computed } from 'vue'

export const useUserStore = defineStore('user', () => {
    const user = ref(JSON.parse(localStorage.getItem('code_user') || '{}'))

    const isLogin = computed(() => !!user.value.id)
    const isAdmin = computed(() => user.value.role === 'admin')
    function loginSuccess(u) {
        user.value = u
        localStorage.setItem('code_user', JSON.stringify(u))
    }

    function logout() {
        user.value = {}
        localStorage.removeItem('code_user')
    }

    return { user, isLogin, isAdmin,loginSuccess, logout }
})
